﻿namespace Sage.MarkdownRenderer.Html
{
    /// <summary>
    /// HTML 脚本提供者
    /// </summary>
    public static class HtmlScriptProvider
    {
        /// <summary>
        /// 获取代码复制脚本
        /// </summary>
        public static string GetCodeCopyScript()
        {
            return @"
                <script>
                // 为代码块添加复制按钮
                document.addEventListener('DOMContentLoaded', function() {
                    const preElements = document.querySelectorAll('pre');
                    preElements.forEach(function(pre) {
                        // 跳过已经处理过的代码块
                        if (pre.parentElement && pre.parentElement.classList.contains('code-container')) {
                            return;
                        }
                        
                        const container = document.createElement('div');
                        container.className = 'code-container';
                        pre.parentNode.insertBefore(container, pre);
                        container.appendChild(pre);
                        
                        const copyBtn = document.createElement('button');
                        copyBtn.className = 'code-copy-btn';
                        copyBtn.textContent = '复制';
                        copyBtn.type = 'button'; // 明确指定按钮类型
                        
                        // 使用 onclick 而不是 addEventListener 避免被禁用脚本影响
                        copyBtn.onclick = function(e) {
                            e.preventDefault();
                            e.stopPropagation();
                            
                            const code = pre.textContent || pre.innerText;
                            
                            // 使用多种复制方法确保兼容性
                            if (navigator.clipboard && navigator.clipboard.writeText) {
                                navigator.clipboard.writeText(code).then(function() {
                                    copyBtn.textContent = '已复制';
                                    setTimeout(function() {
                                        copyBtn.textContent = '复制';
                                    }, 2000);
                                }).catch(function() {
                                    // 备用复制方法
                                    fallbackCopy(code, copyBtn);
                                });
                            } else {
                                // 备用复制方法
                                fallbackCopy(code, copyBtn);
                            }
                        };
                        
                        container.appendChild(copyBtn);
                    });
                    
                    // 备用复制函数
                    function fallbackCopy(text, button) {
                        const textArea = document.createElement('textarea');
                        textArea.value = text;
                        textArea.style.position = 'fixed';
                        textArea.style.left = '-999999px';
                        textArea.style.top = '-999999px';
                        document.body.appendChild(textArea);
                        textArea.focus();
                        textArea.select();
                        
                        try {
                            document.execCommand('copy');
                            button.textContent = '已复制';
                            setTimeout(function() {
                                button.textContent = '复制';
                            }, 2000);
                        } catch (err) {
                            button.textContent = '复制失败';
                            setTimeout(function() {
                                button.textContent = '复制';
                            }, 2000);
                        }
                        
                        document.body.removeChild(textArea);
                    }
                });
                </script>";
        }

        /// <summary>
        /// 获取动画脚本
        /// </summary>
        /// <param name="enableEntryAnimations">是否启用进入动画</param>
        public static string GetAnimationScript(bool enableEntryAnimations = true)
        {
            if (!enableEntryAnimations)
            {
                return @"
                <script>
                // 表格悬停效果增强（无进入动画版本）
                document.addEventListener('DOMContentLoaded', function() {
                    const tables = document.querySelectorAll('table');
                    tables.forEach(function(table) {
                        const rows = table.querySelectorAll('tr');
                        rows.forEach(function(row) {
                            row.addEventListener('mouseenter', function() {
                                this.style.transform = 'scale(1.01)';
                                this.style.zIndex = '1';
                            });
                            row.addEventListener('mouseleave', function() {
                                this.style.transform = 'scale(1)';
                                this.style.zIndex = 'auto';
                            });
                        });
                    });
                });
                </script>";
            }

            return @"
                <script>
                // 添加进入动画
                document.addEventListener('DOMContentLoaded', function() {
                    const elements = document.querySelectorAll('h1, h2, h3, p, blockquote, pre, table');
                    elements.forEach(function(el, index) {
                        el.style.opacity = '0';
                        el.style.transform = 'translateY(20px)';
                        setTimeout(function() {
                            el.style.transition = 'all 0.6s ease';
                            el.style.opacity = '1';
                            el.style.transform = 'translateY(0)';
                        }, index * 100);
                    });
                });
                
                // 表格悬停效果增强
                document.addEventListener('DOMContentLoaded', function() {
                    const tables = document.querySelectorAll('table');
                    tables.forEach(function(table) {
                        const rows = table.querySelectorAll('tr');
                        rows.forEach(function(row) {
                            row.addEventListener('mouseenter', function() {
                                this.style.transform = 'scale(1.01)';
                                this.style.zIndex = '1';
                            });
                            row.addEventListener('mouseleave', function() {
                                this.style.transform = 'scale(1)';
                                this.style.zIndex = 'auto';
                            });
                        });
                    });
                });
                </script>";
        }

        /// <summary>
        /// 获取禁用脚本
        /// </summary>
        public static string GetDisableScript()
        {
            return @"
                <script>
                (function() {
                    // 禁用右键菜单
                    document.addEventListener('contextmenu', function(e) {
                        e.preventDefault();
                        return false;
                    }, true);
                    
                    // 禁用开发者工具快捷键
                    document.addEventListener('keydown', function(e) {
                        if (e.keyCode === 123 || 
                            (e.ctrlKey && e.shiftKey && e.keyCode === 73) ||
                            (e.ctrlKey && e.keyCode === 85) ||
                            (e.ctrlKey && e.shiftKey && e.keyCode === 67)) {
                            e.preventDefault();
                            e.stopPropagation();
                            return false;
                        }
                    }, true);
                    
                    // 禁用选择和拖拽，但排除按钮点击
                    document.addEventListener('selectstart', function(e) {
                        if (e.target.tagName.toLowerCase() === 'button' || 
                            e.target.classList.contains('code-copy-btn')) {
                            return true;
                        }
                        e.preventDefault(); 
                        return false; 
                    }, true);
                    
                    document.addEventListener('dragstart', function(e) { 
                        e.preventDefault(); 
                        return false; 
                    }, true);
                    
                    // 禁用复制粘贴，但排除按钮操作
                    document.addEventListener('copy', function(e) { 
                        if (e.target.tagName.toLowerCase() === 'button' || 
                            e.target.classList.contains('code-copy-btn')) {
                            return true;
                        }
                        e.preventDefault(); 
                    }, true);
                    
                    document.addEventListener('cut', function(e) { e.preventDefault(); }, true);
                    document.addEventListener('paste', function(e) { e.preventDefault(); }, true);
                })();
                </script>";
        }
    }
}
